package cn.net.autocode.platform.design;

import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.platform.design.handle.AutoCodeHandle;
import cn.net.autocode.platform.design.vo.AppInfo;
import cn.net.autocode.platform.design.webclient.AcWebClient;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/design/developerLogin")
public class DeveloperLogin {

    private final AcWebClient acWebClient;

    public DeveloperLogin(AcWebClient acWebClient) {
        this.acWebClient = acWebClient;
    }


    /**
     * 页面登录
     */
    @PostMapping(value = "/login", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultT login(@RequestBody JSONObject param) {
        AppInfo appInfo = AutoCodeHandle.getAppInfo();
        if(appInfo==null){
            return ResultT.fail("没有授权信息，请先获取本项目的license.lic授权证书");
        }



        JSONObject rt = acWebClient.licenseLogin(param);
        if(rt.getIntValue("code")==1){
            JSONObject jj = rt.getJSONObject("data");
            AutoCodeHandle.writeDeveloperInfo(jj);
            acWebClient.clearHeader();
            jj.put("code",appInfo.getCode());
            return ResultT.success(jj);
        }else{
            return ResultT.fail(rt.getString("msg"));
        }
    }


}
